package com.sheng.controller;


import com.github.pagehelper.PageInfo;
import com.sheng.common.Result;
import com.sheng.entity.ApiAuto;
import com.sheng.po.jmeter.JmeterPathPo;
import com.sheng.po.jmeter.JmeterSavePo;
import com.sheng.po.jmeter.PageJmeterQuery;
import com.sheng.service.IApiAutoService;
import com.sheng.util.JmeterUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 王胜
 * @since 2022-12-02 13:31
 */
@RestController
@RequestMapping("/apiAuto")
@Api(tags = "接口自动化相关接口")
@Slf4j
public class ApiAutoController {

    @Autowired
    private IApiAutoService apiAutoService;

    @PostMapping("/runWindows")
    @ApiOperation("windows上运行jmeter文件ByFileName")
    public Result runJmeter(@RequestBody JmeterPathPo jmeterPathPo){

        if (jmeterPathPo.getJmeterPath() != null && !jmeterPathPo.getJmeterPath().equals("")){
            log.info("开始执行windows环境下，接口自动化脚本");
            String reportHtml = JmeterUtil.windowsRun(jmeterPathPo);
            if (reportHtml != null && !reportHtml.equals("")){
                return new  Result(200,"执行完毕",reportHtml);
            }
        }
        return new  Result(500,"执行异常",null);
    }

    @PostMapping("/saveJmxWindows")
    @ApiOperation("保存jmeter脚本在windows环境下")
    public Result saveJmxWindows(@RequestBody JmeterSavePo jmeterSavePo){

        Integer integer = apiAutoService.saveJmxWindows(jmeterSavePo);
        if (integer == 1){
            return new Result(200,"新增成功",null);
        }
        return new Result(500,"新增失败",null);
    }

    @PostMapping("/findPage")
    @ApiOperation("分页模糊查询接口自动化测试用例")
    public Result getUserInfoByPage(@RequestBody PageJmeterQuery pageJmeterQuery){
        PageInfo<ApiAuto> pageInfo = apiAutoService.getJmeterInfoByPage(pageJmeterQuery);
        log.info(pageInfo.toString());
        return new Result<>(200, "查询成功", pageInfo);
    }

    @GetMapping("/runWindowsById/{id}")
    @ApiOperation("通过脚本ID在windows环境下运行")
    public Result runJmWinById(@PathVariable Integer id){
        String  reportUrl = apiAutoService.runJmWinById(id);
        if (reportUrl != null && !reportUrl.equals("")){
            return new Result(200,"运行成功",reportUrl);
        }
        return new Result(500,"运行失败",null);
    }

    @GetMapping("/getJmeterById/{id}")
    @ApiOperation("通过ID获取脚本信息")
    public Result getJmeterById(@PathVariable Integer id){
        ApiAuto jmeterById = apiAutoService.getJmeterById(id);
        return new Result(200,"查询成功",jmeterById);
    }

    @PostMapping("/update")
    @ApiOperation("更新脚本信息")
    public Result updateJmeter(@RequestBody ApiAuto apiAuto ){
        Integer integer = apiAutoService.updateJmeter(apiAuto);
        if (integer == 1){
            return new Result(200,"更新成功",null);
        }
        return new Result(500,"更新失败",null);
    }

    @GetMapping("/delete/{id}")
    @ApiOperation("根据id删除脚本")
    public Result deleteJmeter(@PathVariable Integer id){
        Integer integer = apiAutoService.deleteJmeter(id);
        if (integer == 1){
            return new Result(200,"删除成功",null);
        }
        return new Result(500,"删除失败",null);
    }
}
